package com.aiti.lulian.controller;
/**
 * @Author chenxianzhen
 * @create 2024/6/13 10:35
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.LuLianDockingMeetingDetailsDto;
import com.aiti.lulian.entity.LuLianDockingMeetingDetails;
import com.aiti.lulian.entity.LulianActivty;
import com.aiti.lulian.service.ILulianActivityService;
import com.aiti.lulian.utils.ActivityExcelExportUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @ClassName LuLianActivityController 活动对接
 * @Description TODO
 * @Author chenxianzhen
 * @Date 2024/6/13 10:35
 */
@RestController
@RequestMapping("/activity")
public class LuLianActivityController extends WebController {
    @Resource
    ILulianActivityService lulianActivityService;

    /**
     * 获取home页面云对接数据
     *
     * @return Message
     * @date 2024/6/13 10:37
     * @author chenxianzhen
     */
    @GetMapping("/{baseId}")
    public Message getActivityForHome(@PathVariable("baseId") String baseId) {
        List<LulianActivty> list = lulianActivityService.getHomeData(baseId);
        return Message.success(list);
    }


    /**
     * 查询当前用户创建的活动,分页
     * @param details 查询参数
     * @return
     */
    @GetMapping("/queryCurrentUserActivitiesPage")
    public Message queryCurrentUserActivitiesPage(LuLianDockingMeetingDetailsDto details) {
        String userId = findUserId();
        Integer pageSize = details.getPageSize();
        Integer pageNum = details.getPageNum();
        Page<LuLianDockingMeetingDetails> page = startPage(pageNum, pageSize);
        List<LuLianDockingMeetingDetails> list = lulianActivityService.queryCurrentUserActivitiesPage(page,userId,details);
        return this.Success(this.getPageResult(list, pageNum, pageSize, (int)page.getTotal()));
    }

    /**
     * 查询当前用户下报名中活动列表，无分页
     * @return
     */
    @GetMapping("/queryCurrentUserActivities")
    public Message queryCurrentUserActivities(@RequestParam("roleIds") String roleId){
        String userId = findUserId();
        List<LuLianDockingMeetingDetails> list = lulianActivityService.queryCurrentUserActivities(userId,roleId);
        return Message.success(list);
    }

    /**
     * 创建线下活动
     * @param details 活动详情信息
     * @return 结果
     */
    @PostMapping("/createLineDownActivity")
    public Message createLineDownActivity(@RequestBody LuLianDockingMeetingDetails details){

        //活动时间
        Date meetingDate = details.getMeetingDate();

        DateTime dateTime = DateUtil.offsetDay(meetingDate, -1);
        details.setEntryEndTime(dateTime);

        Date entryStartTime = details.getEntryStartTime();
        Date entryEndTime = details.getEntryEndTime();

        if(meetingDate == null || entryStartTime == null || entryEndTime == null){
            return Message.fail("报名时间、活动时间需要填写");
        }

        if (!entryStartTime.before(entryEndTime)) {
            return Message.fail("报名开始时间需要在结束时间之前");
        }

        if(!entryEndTime.before(meetingDate)){
            return Message.fail("报名时间需要在活动时间之前");
        }

        return lulianActivityService.createLineDownActivity(details,findUserId());
    }

    /**
     * 通过Id获取线下活动详情
     * @param lineDownId 活动Id
     * @return 结果
     */
    @GetMapping("/getLineDownActivityById")
    public Message getLineDownActivityById(@RequestParam(value = "id",required = false) String lineDownId){
        String userId = findUserId();
        return lulianActivityService.getLineDownActivityById(lineDownId,userId);
    }

    /**
     * 修改线下活动的数据
     * @param details 详情
     * @return
     */
    @PutMapping("/updateLineDownActivity")
    public Message updateLineDownActivity(@RequestBody LuLianDockingMeetingDetails details){
        if (details.getMeetingDate() != null && BeanUtil.isNotEmpty(details.getMeetingDate())) {
        //活动时间
        Date meetingDate = details.getMeetingDate();

        DateTime dateTime = DateUtil.offsetDay(meetingDate, -1);
        details.setEntryEndTime(dateTime);

        Date entryStartTime = details.getEntryStartTime();
        Date entryEndTime = details.getEntryEndTime();

        if(meetingDate == null || entryStartTime == null || entryEndTime == null){
            return Message.fail("报名时间、活动时间需要填写");
        }

        if (!entryStartTime.before(entryEndTime)) {
            return Message.fail("报名开始时间需要在结束时间之前");
        }

        if(!entryEndTime.before(meetingDate)){
            return Message.fail("报名时间需要在活动时间之前");
        }
        }

        return lulianActivityService.updateLineDownActivity(details,findUserId());
    }


    @GetMapping("/getPageLineDownActivityList")
    public Message getPageLineDownActivityList(LuLianDockingMeetingDetailsDto detailsDto){
        Integer pageNum = detailsDto.getPageNum();
        Integer pageSize = detailsDto.getPageSize();
        Page<LuLianDockingMeetingDetails> page = startPage(pageNum,pageSize);
        Page<LuLianDockingMeetingDetails> pageList = lulianActivityService.getPageLineDownActivityList(page,detailsDto);
        return Message.success(pageList);
    }

    /**
     * @Author WongMz
     * @CreateDate 2024-09-10
     * @Description: 导出活动列表为Excel
     */
    @ApiOperation("导出活动列表为Excel")
    @GetMapping("/list/export")
    public Message exportActivityList(HttpServletResponse response, LuLianDockingMeetingDetailsDto details, String userId) {
        try {
            List<LuLianDockingMeetingDetails> list = lulianActivityService.queryCurrentUserActivitiesPage(null, userId, details);
            ActivityExcelExportUtil.exportExcel(response, list);
            return Success("导出成功！");
        } catch (Exception e) {
            logger.error("【exportActivityList】方法导出异常：===》" + e);
            return Fail(e.getMessage());
        }
    }
}
